<?php

namespace Admin\Controller;

class SliderController extends AdminController
{
    /**
     * 后台菜单轮播图首页
     * @author Weiliqiang <[<email address>]>
     * @return [type] [description]
     */
    public function index()
    {
        
        // $list = D('Slider')->where('status in (0,1)')->order('sort asc,id asc')->select();

        // if($list) {
        //     $this->assign('list',$list);
        // }

        $map['status'] = array('egt','0');
        $list = $this->lists('Slider',$map);
        $this->assign('list',$list);

        // 记录当前列表页的cookie
        Cookie('__forward__',$_SERVER['REQUEST_URI']);

        $this->meta_title = '广告列表';
        $this->display();
    }

    /**
     * 新增轮播图
     * @author Weiliqiang <[<email address>]>
     */
    public function add()
    {
        if(IS_POST){
            $Slider = D('Slider');
            $data = $Slider->create();

            if($data){
                // 先上传图片，文件上传时字段一定不能为空
                $config = array(
                'maxSize'    =>    3145728,    
                'savePath'   =>    './Picture/Slider/',  // 默认保存至Uploads文件夹下  
                'saveName'   =>    array('uniqid',''),    
                'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),    
                'autoSub'    =>    true,    //开启子目录保存
                'subName'    =>    array('date','Ymd'),
                );
                $upload = new \Think\Upload($config);// 实例化上传类
                $info = $upload->upload();
                
                // 获取保存的文件名和保存路径
                foreach($info as $file) {
                    $data['save_name'] = $file['savename'];
                    $data['save_path'] = $file['savepath'];
                }

                $id = $Slider->add($data);

                if($id) {
                    $this->success('新增成功', U('index'));
                    //记录行为
                    action_log('update_Slider', 'Slider', $id, UID);
                } else {
                    $this->error('新增失败');
                }
            } else {
                $this->error($Slider->getError());
            }
        } else {
            $this->assign('info',null);
            $this->meta_title = '新增广告';
            $this->display('edit');
        }
    }

    /**
     * 广告图删除
     * @author Weiliqiang <[<email address>]>
     * @return [type] [description]
     */
    public function del(){
        $id = array_unique((array)I('id',0));

        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }

        $map = array('id' => array('in', $id) );

        // 注意：删除文件夹里的图片，作废，用假性删除，即将状态码改为 -1
        /* 
            foreach($info as $key=>$val){
                $del_path = 'Uploads'.ltrim($val['save_path'].$val['save_name'],'.');
                unlink($del_path);
            }
        */

        // 调用AdminController中的delete假性删除方法
        $res = $this->delete('Slider', $map); 

        if($res){
            //记录行为
            action_log('update_slider', 'channel', $id, UID);
            $this->success('删除成功');
        } else {
            $this->error('删除失败！');
        }
    }

    /**
     * 广告图片编辑
     * @author Weiliqiang <[<email address>]>
     * @param  integer $id [description]
     * @return [type]      [description]
     */
    public function edit($id = 0){
        if(IS_POST){
            $slider = D('slider');
            $id = I('post.id');
            $data = $slider->create();
            if($data){
                // 若修改了图片，则上传图片后删除原图
                if($_FILES['picture']['error'] != 4) {
                    $config = array(
                    'maxSize'    =>    3145728,    
                    'savePath'   =>    './Picture/Slider/',  // 默认保存至Uploads文件夹下  
                    'saveName'   =>    array('uniqid',''),    
                    'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),    
                    'autoSub'    =>    true,    //开启子目录保存
                    'subName'    =>    array('date','Ymd'),
                    );
                    $upload = new \Think\Upload($config);// 实例化上传类
                    $info = $upload->upload();
                    
                    // 获取保存的文件名和保存路径
                    foreach($info as $file) {
                        $data['save_name'] = $file['savename'];
                        $data['save_path'] = $file['savepath'];
                    }
                }else{
                    // 若没有修改图片，则将原来的图片信息传回
                    $old_info = $slider->find($id);
                    $data['save_name'] = $old_info['save_name'];
                    $data['save_path'] = $old_info['save_path'];
                }

                if($slider->where('id='.$id)->save($data)){
                    //记录行为
                    action_log('update_slider', 'slider', $data['id'], UID);
                    $this->success('编辑成功', U('index'));
                } else {
                    $this->error('编辑失败');
                }
            } else {
                $this->error($slider->getError());
            }
        } else {
            $info = array();
            /* 获取数据 */
            $info = M('slider')->find($id);

            if(false === $info){
                $this->error('获取信息错误');
            }

            $this->assign('info', $info);
            $this->meta_title = '编辑广告图片';
            $this->display();
        }
    }
}